﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;

using com.ubosi.common;

namespace com.ubosi.spider.config
{
    public sealed class SystemConfigManager
    {
        private static SystemConfigManager _theManager = null;
        private int TimeoutMillis = 2000; //定时器触发间隔
        System.Threading.Timer _timer = null;
        List<String> files = new List<string>();
        private FileSystemWatcher _watcher;
        private SystemConfig _systemConfig;

        private SystemConfigManager()
        {
            LoadSpiderConfig(Path.Combine(Environment.CurrentDirectory + "\\Config\\", "NetSpider.xml"));

            if (_timer == null)
            {
                //设置定时器的回调函数。此时定时器未启动       
                _timer = new System.Threading.Timer(new TimerCallback(OnWatchedFileChange), null, Timeout.Infinite, Timeout.Infinite);
            }

            _watcher = new FileSystemWatcher(Environment.CurrentDirectory + "\\Config\\", "NetSpider.xml");
            _watcher.NotifyFilter = NotifyFilters.LastWrite;
            _watcher.IncludeSubdirectories = false;
            _watcher.Changed += new FileSystemEventHandler(_watcher_Changed);
            _watcher.EnableRaisingEvents = true;



        }
        private void OnWatchedFileChange(object state) 
        { 
            List<String> backup = new List<string>(); 
            Mutex mutex = new Mutex(false, "FSW"); 
            mutex.WaitOne(); 
            backup.AddRange(files); 
            files.Clear(); 
            mutex.ReleaseMutex(); 
            foreach (string file in backup)
            {
                if (string.Compare(file, Path.Combine(Environment.CurrentDirectory + "\\Config\\", "NetSpider.xml"), true) == 0)
                {
                    LoadSpiderConfig(file);
                }                
            }
            
            
        }
        private void OnWatchedFileChange()
        {
           
        }

        static SystemConfigManager()
        {
            _theManager = new SystemConfigManager();
          
        }

       
      
        public static SystemConfig SystemConfig
        {
            get
            {
                return _theManager._systemConfig;
            }
        }



        private void _watcher_Changed(object sender, FileSystemEventArgs e)
        {
            Mutex mutex = new Mutex(false, "FSW");
            mutex.WaitOne();
            if (!files.Contains(e.FullPath))
            {
                files.Add(e.FullPath);
            }
            mutex.ReleaseMutex();
            //重新设置定时器的触发间隔，并且仅仅触发一次           
            _timer.Change(TimeoutMillis, Timeout.Infinite);          
        }

        private void LoadSpiderConfig(string configFile)
        {

            SystemConfig config = SerializerUtility.DeSerializeFromFile(typeof(SystemConfig), configFile) as SystemConfig;
            if (config != null)
                _systemConfig = config;
      
        }
    }
}
